﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using ChatClient.Resources;

namespace ChatClient
{
    public partial class MainPage : PhoneApplicationPage
    {
        private bool Connected = false;
        ConnectServer Client = new ConnectServer();

        public MainPage self;

        public MainPage getInstance()
        {
            return self;
        }

        // Constructor
        public MainPage()
        {
            InitializeComponent();
            self = this;
            // Sample code to localize the ApplicationBar
            //BuildLocalizedApplicationBar();
        }

        public void displayConnectTo(string hostname)
        {
            
        }

        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

        private void btnSend_Click(object sender, RoutedEventArgs e)
        {
            String s = txtChatBox.Text;
            Client.SendData(s);
            txtChatBox.Text = "";
        }

        private void btnConnect_Click(object sender, RoutedEventArgs e)
        {
            Global.Connected = !Global.Connected;
            if (Global.Connected) 
            {
                //ket noi server
                btnConnect.Content = "DISCONNECT";
            }
            else
            {
                //da ngat ket noi
                btnConnect.Content = "CONNECT";
            }
            Client.connectToServer();
            Client.ResponseReceived += Client_ResponseReceived;
        }

        void Client_ResponseReceived(object sender, ResponseReceivedEventArgs e)
        {
            string s = e.response;
            if (System.Windows.Deployment.Current.Dispatcher.CheckAccess())
            {
                // Since I want to call the same logic here and in the Dispatcher case, 
                // I encapsulate it in a method they can both call.
                changeText(s);
            }
            else
            {
                System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    changeText(s);
                });
            }
        }

        void changeText(string s)
        {
            txtMessage.Text += s + "\n";
        }

        // Sample code for building a localized ApplicationBar
        //private void BuildLocalizedApplicationBar()
        //{
        //    // Set the page's ApplicationBar to a new instance of ApplicationBar.
        //    ApplicationBar = new ApplicationBar();

        //    // Create a new button and set the text value to the localized string from AppResources.
        //    ApplicationBarIconButton appBarButton = new ApplicationBarIconButton(new Uri("/Assets/AppBar/appbar.add.rest.png", UriKind.Relative));
        //    appBarButton.Text = AppResources.AppBarButtonText;
        //    ApplicationBar.Buttons.Add(appBarButton);

        //    // Create a new menu item with the localized string from AppResources.
        //    ApplicationBarMenuItem appBarMenuItem = new ApplicationBarMenuItem(AppResources.AppBarMenuItemText);
        //    ApplicationBar.MenuItems.Add(appBarMenuItem);
        //}
    }
}